﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bank
{
    public partial class MainForm : Form
    {
        enum eColums
        {
            COL_LINE_ID = 0,
            COL_LINE_TYPE,
            COL_STATE,
            COL_CALLER,
            COL_DTMF,
            COL_MAX,
        }
        enum EnumStep
        {
            STEP_IDLE = 0,
            STEP_WELCOME,
            STEP_INPUT_COUNT,
            STEP_INPUT_PASS,
            STEP_CONSULT_DETAIL,
            STEP_GOODBY,
        }

        ArrayList ChannelArray;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Phonic_usb.OpenDevice(0) != 0)
            {
                MessageBox.Show("Open Device Error!");
                return;
            }

            Phonic_usb.OpenLogFile(0);
            ChannelArray = new ArrayList();  

            Linelist.GridLines = true; //显示表格线
            Linelist.View = View.Details;//显示表格细节
            Linelist.FullRowSelect = true;//是否可以选择行
            Linelist.Columns.Add("ID", 60);
            Linelist.Columns.Add("类型", 60);
            Linelist.Columns.Add("状态", 60);
            Linelist.Columns.Add("主叫", 100);
            Linelist.Columns.Add("DTMF", 100);
        }
        private void MainForm_Closing(object sender, FormClosingEventArgs e)
        {
            Phonic_usb.CloseLogFile();
            Phonic_usb.CloseDevice();
        }

        private Channel GetChannel(int handle)
        {
            foreach (Channel TheChannel in ChannelArray)
            {
                if (TheChannel._handle == handle)
                {
                    return TheChannel;
                }
            }
            return null;
        }

        private void OnPlugIn(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PLugInEvent e)
        {
            Channel ThisChannel = new Channel();
            ThisChannel.Inital(e.uboxHandle, ChannelArray.Count);
            int lineType = Phonic_usb.GetProductID(e.uboxHandle);
            string strLineType = "";
            switch (lineType)
            {
			case 1:
                strLineType = "FI3102A";
				break;
			case 2:
                strLineType = "FI3002B";
				break;
			case 3:
                strLineType = "FI3001B";
					break;
			case 4:
                strLineType = "FI3101A";
				break;
			default:
                strLineType = "unkown product";
                break;
			}

            ListViewItem p = new ListViewItem(new string[] { ChannelArray.Count.ToString(), strLineType,"空闲", "", "" });
            p.Tag = ThisChannel;
            Linelist.Items.Add(p);

            ChannelArray.Add(ThisChannel);

        }

        private void OnPlugOut(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PlugOutEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                foreach (ListViewItem item in Linelist.Items)
                {
                    if ((Channel)item.Tag == ThisChannel)
                    {
                        item.Remove();
                        ChannelArray.Remove(ThisChannel);
                        break;
                     }
                }
                for(int i=0;i<Linelist.Items.Count;i++)
                {
                    Linelist.Items[i].SubItems[(int)eColums.COL_LINE_ID].Text = i.ToString();
                }
            }
        }
        private void OnDeviceAlarm(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DeviceAlarmEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                string strMsg = "UBOX ";
                strMsg  +=  ThisChannel._lineID.ToString();
                switch(e.param)
                {
                case 1:	
                    strMsg += "未能找到ubox 的MIC 设备!";
                    break;
                case 2:	
                    strMsg += "未能打开ubox 的MIC 设备!";
                    break;
                case 3:
                    strMsg += "未能打开ubox 的放音设备!";
                    break;
                case 4:	
                    strMsg += "设备故障!";
                    break;
                }
                MessageBox.Show(strMsg);

            }            
        }
        private void OnDeviceError(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DeviceErrorEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                string strMsg = "UBOX ";
                strMsg += ThisChannel._lineID.ToString();
                strMsg += " 设备错误!";
                MessageBox.Show(strMsg);

            }            
        }


        private void OnRing(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_RingEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                ThisChannel._ringCount++;
                string strTemp = "振铃 ";

                strTemp += ThisChannel._ringCount.ToString();
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = strTemp;
                if (ThisChannel._ringCount >= 3)
                {
                    Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "通话";
                    Phonic_usb.SoftHookoff(ThisChannel._handle);
                    Phonic_usb.PlayFile(ThisChannel._handle, "voice\\欢迎.wav");
                    ThisChannel._step = (int)EnumStep.STEP_WELCOME;
                }
            }            
        }

        private void OnRingCancel(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_RingCancelEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "空闲";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
                ThisChannel._ringCount = 0;
            }            
        }
        private void OnCallId(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_CallIdEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "通话";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = e.callerNumber;
                Phonic_usb.SoftHookoff(ThisChannel._handle);
                Phonic_usb.PlayFile(ThisChannel._handle, "voice\\欢迎.wav");
                ThisChannel._step = (int)EnumStep.STEP_WELCOME;
            }            
        }


        private void OnDtmfKey(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DtmfKeyEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                ThisChannel._strDTMF += (char)e.dtmfCode;
                switch (ThisChannel._step)
                {
                    case (int)EnumStep.STEP_INPUT_COUNT:
                        if (ThisChannel._strDTMF.Length >= 8)
                        {
                            ThisChannel._step = (int)EnumStep.STEP_INPUT_PASS;
                            Phonic_usb.PlayFile(ThisChannel._handle, "voice\\密码.wav");
                            ThisChannel._strDTMF = "";
                        }
                        break;
                    case (int)EnumStep.STEP_INPUT_PASS:
                        if (ThisChannel._strDTMF.Length >= 6)
                        {
                            ThisChannel._step = (int)EnumStep.STEP_CONSULT_DETAIL;
                            Phonic_usb.PlayFile(ThisChannel._handle, "voice\\具体内容.wav");
                            ThisChannel._strDTMF = "";
                        }
                        break;
                    case (int)EnumStep.STEP_CONSULT_DETAIL:
                        switch ((char)e.dtmfCode)
                        {
                            case '1':
                                ThisChannel._step = (int)EnumStep.STEP_GOODBY;
                                Phonic_usb.PlayFile(ThisChannel._handle, "voice\\余额是.wav");
                                break;
                            case '2':
                                ThisChannel._step = (int)EnumStep.STEP_GOODBY;
                                Phonic_usb.PlayFile(ThisChannel._handle, "voice\\利率是.wav");
                                break;
                            case '3':
                                ThisChannel._step = (int)EnumStep.STEP_GOODBY;
                                Phonic_usb.PlayFile(ThisChannel._handle, "voice\\利息是.wav");
                                break;
                        }
                        break;

                }
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = ThisChannel._strDTMF;
            }            
        }


        private void OnPlayEnd(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PlayEndEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                switch (ThisChannel._step)
                {
                    case (int)EnumStep.STEP_WELCOME:
                        ThisChannel._step = (int)EnumStep.STEP_INPUT_COUNT;
                        Phonic_usb.PlayFile(ThisChannel._handle, "voice\\账号.wav");
                        break;
                    case(int)EnumStep.STEP_GOODBY:
                        Phonic_usb.PlayFile(ThisChannel._handle, "voice\\再见.wav");
                        break;
                }
            }
        }

        private void OnHangup(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_HangUpEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "空闲";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
                ThisChannel._ringCount = 0;
            }
        }

        private void OnHookOff(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_HookOffEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "摘机";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
            }
        }


        private void OnToneBusy(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_ToneBusyEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Phonic_usb.StopPlay(ThisChannel._handle);
                Phonic_usb.SoftHookon(ThisChannel._handle);
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
            }
        }


    }
}
